from odoo import api, fields, models
from odoo.exceptions import UserError


class VaccineInformationEntering(models.Model):
    _name = "get.vaccine"
    _description = "获取被标记问题疫苗的相关信息"

    # 疫苗ID
    vaccine_id = fields.Char('疫苗ID', required=True)

    # 疫苗名称

    name = fields.Char(
        string='疫苗中文名',
        required=True
    )
    name_en = fields.Char(
        string='疫苗英文名'
    )
    # 疫苗类别
    vaccine_category = fields.Selection(
        [
            ('1', 'HPV疫苗'),
            ('2', '卡介苗'),
            ('3', '小儿麻痹疫苗'),
            ('4', '狂犬疫苗'),
            ('5', '乙肝疫苗'),
            ('6', '水痘疫苗'),
            ('7', '其他'),
        ],
        string='疫苗类别',
        required=True
    )

    # 疫苗属性
    vaccine_attribute = fields.Selection(
        [
            ('1', '第一类疫苗'),
            ('2', '第二类疫苗')
        ],
        string='疫苗属性',
        required=True
    )

    # 单位名称
    organization_name = fields.Char('单位名称', required=True)

    # 日期
    date = fields.Date('日期', required=True)

    # 生产企业
    manufacturer = fields.Many2one('factory', string='生产企业', required=True)

    # 生产批号
    batch_number = fields.Char('生产批号', required=True)

    # 批准文号
    allow_number = fields.Char('批准文号', required=True)

    # 有效期
    period_validity = fields.Char('有效期(天)', required=True)

    # 单据类型（勾选）
    document_type = fields.Selection(
        [
            ('1', '出库'),
            ('2', '未出库'),
        ],
        string='单据类型',
        required=True
    )

    # 剂型
    vaccine_form = fields.Selection(
        [
            ('1', '液体安剖瓶'),
            ('2', '冻干西林瓶'),
            ('3', '其他'),
        ],
        string='剂型',
        required=True
    )

    # 规格
    standard = fields.Selection(
        [
            ('1', '0.5ml/支'),
            ('2', '1.0ml/支'),
            ('3', '1.5ml/支'),
            ('4', '2.0ml/支'),
            ('5', '2.5ml/支'),
            ('6', '其他'),
        ],
        string='规格',
        required=True
    )

    # 入库量
    receipt = fields.Char('入库量', required=True)

    # 出库量
    outbound_quantity = fields.Char('出库量', required=True)

    # 接种人数
    vaccine_number = fields.Char('接种人数', required=True)

    # 本批结余
    surplus = fields.Char('本批结余', required=True)

    # 结余量
    surplus_number = fields.Char('结余量', required=True)

    # 经手人
    reponsible_number = fields.Many2one(
        'res.users',
        string='经手人',
        required=True
    )
    is_bad = fields.Boolean(
        '是否为问题疫苗',
        default=True
    )
    priority = fields.Selection(
        [('0', '平级'),
         ('1', '紧急'),
         ('2', '严重')],
        '问题等级',
        default='1')
    color = fields.Integer()

    def no_problem(self):
        return {
            'type': 'ir.actions.act_window',
            'view_type': 'form',
            'view_mode': 'form,tree',
            'res_model': 'no.problem',
            'target': 'new',
            'context': {
                'default_vaccine_id': self.vaccine_id,
                'default_name': self.name,
                'default_name_en': self.name_en,
                'default_vaccine_category': self.vaccine_category,
                'default_vaccine_attribute': self.vaccine_attribute,
                'default_organization_name': self.organization_name,
                'default_date': self.date,
                'default_manufacturer': self.manufacturer.id,
                'default_batch_number': self.batch_number,
                'default_allow_number': self.allow_number,
                'default_period_validity': self.period_validity,
                'default_document_type': self.document_type,
                'default_vaccine_form': self.vaccine_form,
                'default_standard': self.standard,
                'default_receipt': self.receipt,
                'default_outbound_quantity': self.outbound_quantity,
                'default_vaccine_number': self.vaccine_number,
                'default_surplus': self.surplus,
                'default_surplus_number': self.surplus_number,
                'default_reponsible_number': self.reponsible_number.id,
            }
        }

    def search_duty(self):
        return {
            'type': 'ir.actions.act_window',
            'view_type': 'form',
            'view_mode': 'form,tree',
            'res_model': 'duty.search',
            'target': 'new',
            'context': {
                'default_vaccine_id': self.vaccine_id,
                'default_name': self.name,
                'default_vaccine_category': self.vaccine_category,
                'default_organization_name': self.organization_name,
                'default_manufacturer': self.manufacturer.id,
                'default_batch_number': self.batch_number,
                'default_standard': self.standard,

            }
        }

